<html>
<head><meta charset="utf-8"><title>Lint idea: `into_iter()` over `drain(..)` · clippy · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/index.html">clippy</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Lint.20idea.3A.20.60into_iter().60.20over.20.60drain(.2E.2E).60.html">Lint idea: `into_iter()` over `drain(..)`</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="242575066"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Lint%20idea%3A%20%60into_iter%28%29%60%20over%20%60drain%28..%29%60/near/242575066" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Lint.20idea.3A.20.60into_iter().60.20over.20.60drain(.2E.2E).60.html#242575066">(Jun 14 2021 at 09:59)</a>:</h4>
<p>I saw this today in a code review, where someone used <code>vec.drain(..).map(...).other_iterator_methods()</code> to transform the vec into an owned iterator.  In this case, using <code>into_iter()</code> is definitely the idiomatic thing to do. Now I wonder if there are cases where using <code>drain(..)</code> over <code>into_iter()</code>  makes sense. The <a href="https://doc.rust-lang.org/std/vec/struct.Drain.html"><code>Drain</code></a> struct provides the same methods/trait impls as the <a href="https://doc.rust-lang.org/std/vec/struct.IntoIter.html"><code>IntoIter</code></a> struct, so you shouldn't get into trouble that a method called after <code>drain(..)</code> isn't available anymore.</p>
<p>The only thing that should be checked is if <code>vec</code> is used after <code>vec.drain(..)</code>. That's the only case where using <code>drain(..)</code> over <code>into_iter()</code> makes sense to me.</p>
<p>What do you think? Should we add a <code>style</code> (?) lint for this? If so, I'd be happy to implement it.</p>



<a name="242620431"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Lint%20idea%3A%20%60into_iter%28%29%60%20over%20%60drain%28..%29%60/near/242620431" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Lint.20idea.3A.20.60into_iter().60.20over.20.60drain(.2E.2E).60.html#242620431">(Jun 14 2021 at 16:03)</a>:</h4>
<p>They should be equivalent as long as the full range is used by <code>drain(..)</code>. The only difference could be the drop order, as <code>drain(..)</code> is <a href="https://doc.rust-lang.org/std/vec/struct.Vec.html#method.drain">documented</a> to only remove the elements from the vec once the <code>Drain</code> struct is dropped, while <code>IntoIter</code> has no comment in that regard. However, the vector can not be used while it's being mutably borrowed anyways so that shouldn't matter. So <span aria-label="+1" class="emoji emoji-1f44d" role="img" title="+1">:+1:</span> from me <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>